﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PRET.ReportDataDefine.UIData
{
    public static class DataConvert
    {
        /// <summary>
        /// 将WinCC里面的生产报表数据转化成前台支持的数据格式
        /// </summary>
        /// <param name="winccList"></param>
        /// <param name="batchNum"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="estimateEndTime"></param>
        /// <returns></returns>
        public static ProductionReport Convert2ProductionReport(List<Dictionary<string, string>> winccList, string batchNum, DateTime startTime, DateTime? endTime)
        {
            if (winccList.Count == 0)
                return null;

            ProductionReport pr = new ProductionReport();
            pr.Summary = Convert2ProductionReportSummary(winccList[0], batchNum, startTime, endTime);
            pr.DataList = Convert2ProductionReportDetailInfoList(winccList);
            return pr;
        }

        /// <summary>
        /// 将WinCC里面的生产报表摘要数据转化成前台支持的数据格式
        /// </summary>
        /// <param name="winccDic"></param>
        /// <param name="batchNum"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="estimateEndTime"></param>
        /// <returns></returns>
        private static ProductionReportSummary Convert2ProductionReportSummary(Dictionary<string, string> winccDic, string batchNum, DateTime startTime, DateTime? endTime)
        {
            ProductionReportSummary psSummary = new ProductionReportSummary();
            psSummary.BatchID = batchNum;
            psSummary.StartTime = startTime;
            psSummary.EndTime = endTime;
            psSummary.ProductionLine = winccDic["ProLine"];
            psSummary.OrderID = winccDic["OrderNum"];
            psSummary.Bal1Scale = double.Parse(winccDic["BalScale1"]) / 1000;
            psSummary.Bal1TargetCount = double.Parse(winccDic["BalTCount1"]);
            psSummary.Bal2Scale = double.Parse(winccDic["BalScale2"]) / 1000;
            psSummary.Bal2TargetCount = double.Parse(winccDic["BalTCount2"]);
            psSummary.Bal3Scale = double.Parse(winccDic["BalScale3"]) / 1000;
            psSummary.Bal3TargetCount = double.Parse(winccDic["BalTCount3"]);
            psSummary.MaxRongZhi = double.Parse(winccDic["MaxRZ"]);
            psSummary.MinRongZhi = double.Parse(winccDic["MinRZ"]);
            psSummary.MaxRemnant = double.Parse(winccDic["MaxRemn"]);
            psSummary.MinRemnant = double.Parse(winccDic["MinRemn"]);
            psSummary.JCJ1TempOffset = double.Parse(winccDic["JTemOff1"]);
            psSummary.JCJ2TempOffset = double.Parse(winccDic["JTemOff2"]);
            psSummary.JCJ3TempOffset = double.Parse(winccDic["JTemOff3"]);
            psSummary.JCJ4TempOffset = double.Parse(winccDic["JTemOff4"]);
            psSummary.JCJ5TempOffset = double.Parse(winccDic["JTemOff5"]);
            psSummary.JCJ6TempOffset = double.Parse(winccDic["JTemOff6"]);
            psSummary.JCJ7TempOffset = double.Parse(winccDic["JTemOff7"]);
            psSummary.JCJ8TempOffset = double.Parse(winccDic["JTemOff8"]);
            psSummary.JCJ9TempOffset = double.Parse(winccDic["JTemOff9"]);
            psSummary.JCJ10TempOffset = double.Parse(winccDic["JTemOff10"]);
            psSummary.JCJ1SetTemp = double.Parse(winccDic["JTemSet1"]);
            psSummary.JCJ2SetTemp = double.Parse(winccDic["JTemSet2"]);
            psSummary.JCJ3SetTemp = double.Parse(winccDic["JTemSet3"]);
            psSummary.JCJ4SetTemp = double.Parse(winccDic["JTemSet4"]);
            psSummary.JCJ5SetTemp = double.Parse(winccDic["JTemSet5"]);
            psSummary.JCJ6SetTemp = double.Parse(winccDic["JTemSet6"]);
            psSummary.JCJ7SetTemp = double.Parse(winccDic["JTemSet7"]);
            psSummary.JCJ8SetTemp = double.Parse(winccDic["JTemSet8"]);
            psSummary.JCJ9SetTemp = double.Parse(winccDic["JTemSet9"]);
            psSummary.JCJ10SetTemp = double.Parse(winccDic["JTemSet10"]);
            psSummary.ExtruderMaxRevolvingSpeed = double.Parse(winccDic["JMaxRev"]);
            psSummary.ExtruderMinRevolvingSpeed = double.Parse(winccDic["JMinRev"]);
            psSummary.ExtruderMaxCurrent = double.Parse(winccDic["JMaxCur"]);
            psSummary.ExtruderMinCurrent = double.Parse(winccDic["JMinCur"]);
            return psSummary;
        }

        /// <summary>
        /// 将WinCC里面的生产报表详细数据转化成前台支持的数据格式
        /// </summary>
        /// <param name="winccList"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private static List<ProductionReportDetailInfo> Convert2ProductionReportDetailInfoList(List<Dictionary<string, string>> winccList)
        {
            List<ProductionReportDetailInfo> prList = new List<ProductionReportDetailInfo>();
            foreach(Dictionary<string, string> dic in winccList)
            {
                ProductionReportDetailInfo pr = new ProductionReportDetailInfo();
                pr.RecordTime = DateTime.Parse(dic["Time"]);
                pr.WLJRevolvingSpeed = double.Parse(dic["WLJRev"]);
                pr.RealRongZhi = double.Parse(dic["RealRz"]);
                pr.RealRemnant = double.Parse(dic["RealRemn"]);
                pr.JCJ1RealTemp = double.Parse(dic["JTemReal1"]);
                pr.JCJ2RealTemp = double.Parse(dic["JTemReal2"]);
                pr.JCJ3RealTemp = double.Parse(dic["JTemReal3"]);
                pr.JCJ4RealTemp = double.Parse(dic["JTemReal4"]);
                pr.JCJ5RealTemp = double.Parse(dic["JTemReal5"]);
                pr.JCJ6RealTemp = double.Parse(dic["JTemReal6"]);
                pr.JCJ7RealTemp = double.Parse(dic["JTemReal7"]);
                pr.JCJ8RealTemp = double.Parse(dic["JTemReal8"]);
                pr.JCJ9RealTemp = double.Parse(dic["JTemReal9"]);
                pr.JCJ10RealTemp = double.Parse(dic["JTemReal10"]);
                pr.ExtruderRevolvingRealSpeed = double.Parse(dic["JRealRev"]);
                pr.ExtruderRealCurrent = double.Parse(dic["JRealCur"]) / 10;
                prList.Add(pr);
            }
            return prList;
        }

        /// <summary>
        /// 将WinCC里面的干燥均化机摘要数据转化成前台支持的数据格式
        /// </summary>
        /// <param name="winccDic"></param>
        /// <returns></returns>
        private static ProductionSummary Convert2ProductionSummary(Dictionary<string, string> winccDic)
        {
            ProductionSummary ps = new ProductionSummary();
            ps.ProductionLine = winccDic["ProLine"];
            return ps;
        }
        public static ProductionLineEfficiencyReport Convert2ProductionLineEfficiency(ProductionEfficiency pe)
        {
            ProductionLineEfficiencyReport obj = new ProductionLineEfficiencyReport();
            obj.ProductionLine = pe.ProductionLine;
            double maxEffic = double.MinValue;
            double minEffic = double.MaxValue;
            double sumWeight = 0;
            double sumHours = 0;
            // 循环12个月的订单列表，算出每月的效率
            foreach (KeyValuePair<int, List<OrderProductionLineTime>> item in pe.OutPutDic)
            {
                double monthHours = 0;
                double monthWeight = 0;
                if (item.Value != null && item.Value.Count > 0)
                {
                    foreach (OrderProductionLineTime order in item.Value)
                    {
                        sumHours += (order.EndTime - order.StartTime).TotalHours;
                        monthHours += (order.EndTime - order.StartTime).TotalHours;
                        sumWeight += order.Weight;
                        monthWeight += order.Weight;
                    }
                    double efficiency = 0;
                    if (monthHours != 0)
                    {
                        efficiency = Math.Round((monthWeight / monthHours), 0);
                        maxEffic = maxEffic < efficiency ? efficiency : maxEffic;
                        if (efficiency > 0)
                            minEffic = minEffic > efficiency ? efficiency : minEffic;
                    }
                    switch (item.Key)
                    {
                        case 0:
                            obj.JanEfficiency = efficiency;
                            obj.JanHours = monthHours;
                            obj.JanWeight = monthWeight;
                            break;
                        case 1:
                            obj.FebEfficiency = efficiency;
                            obj.FebHours = monthHours;
                            obj.FebWeight = monthWeight;
                            break;
                        case 2:
                            obj.MarEfficiency = efficiency;
                            obj.MarHours = monthHours;
                            obj.MarWeight = monthWeight;
                            break;
                        case 3:
                            obj.AprEfficiency = efficiency;
                            obj.AprHours = monthHours;
                            obj.AprWeight = monthWeight;
                            break;
                        case 4:
                            obj.MayEfficiency = efficiency;
                            obj.MayHours = monthHours;
                            obj.MayWeight = monthWeight;
                            break;
                        case 5:
                            obj.JunEfficiency = efficiency;
                            obj.JunHours = monthHours;
                            obj.JunWeight = monthWeight;
                            break;
                        case 6:
                            obj.JulEfficiency = efficiency;
                            obj.JulHours = monthHours;
                            obj.JulWeight = monthWeight;
                            break;
                        case 7:
                            obj.AugEfficiency = efficiency;
                            obj.AugHours = monthHours;
                            obj.AugWeight = monthWeight;
                            break;
                        case 8:
                            obj.SepEfficiency = efficiency;
                            obj.SepHours = monthHours;
                            obj.SepWeight = monthWeight;
                            break;
                        case 9:
                            obj.OctEfficiency = efficiency;
                            obj.OctHours = monthHours;
                            obj.OctWeight = monthWeight;
                            break;
                        case 10:
                            obj.NovEfficiency = efficiency;
                            obj.NovHours = monthHours;
                            obj.NovWeight = monthWeight;
                            break;
                        case 11:
                            obj.DecEfficiency = efficiency;
                            obj.DecHours = monthHours;
                            obj.DecWeight = monthWeight;
                            break;
                    }
                }                
            }
            if (sumHours != 0 && sumWeight != 0)
                obj.AvgEfficiency = Math.Round((sumWeight / sumHours), 0);
            obj.CurrEfficiency = obj.AvgEfficiency;
            obj.Hours = Math.Round(sumHours, 0);
            obj.Weight = Math.Round(sumWeight, 0);
            if (maxEffic != double.MinValue)
                obj.MaxEfficiency = Math.Round(maxEffic, 0);
            if (minEffic != double.MaxValue)
                obj.MinEfficiency = Math.Round(minEffic, 0);
            return obj;
        }
        private static Dictionary<int, List<TeamProductionLineTime>> Convert2TeamEfficiencyMonthList(List<TeamProductionLineTime> outputList)
        {
            Dictionary<int, List<TeamProductionLineTime>> dic = new Dictionary<int, List<TeamProductionLineTime>>();
            foreach (TeamProductionLineTime output in outputList)
            {
                if (dic.ContainsKey(output.EndTime.Month))
                {
                    dic[output.EndTime.Month].Add(output);
                }
                else
                {
                    dic.Add(output.EndTime.Month, new List<TeamProductionLineTime> { output });
                }
            }
            return dic;
        }
        private static Dictionary<int, List<OrderProductionLineTime>> Convert2ProLineEfficiencyMonthList(List<OrderProductionLineTime> outputList)
        {
            Dictionary<int, List<OrderProductionLineTime>> dic = new Dictionary<int, List<OrderProductionLineTime>>();
            if (outputList == null)
                return dic;
            foreach (OrderProductionLineTime output in outputList)
            {
                if (dic.ContainsKey(output.EndTime.Month))
                {
                    dic[output.EndTime.Month].Add(output);
                }
                else
                {
                    dic.Add(output.EndTime.Month, new List<OrderProductionLineTime> { output });
                }
            }
            return dic;
        }

        public static TeamEfficiencyReport Convert2TeamEfficiency(TeamEfficiency te)
        {
            TeamEfficiencyReport obj = new TeamEfficiencyReport();
            obj.TeamName = te.TeamID;
            if (te.OutPutList == null || te.OutPutList.Count == 0)
                return obj;

            // 计算出这个班组在每个月的生产效率
            Dictionary<int, List<TeamProductionLineTime>> monthList = Convert2TeamEfficiencyMonthList(te.OutPutList);
            double maxEffic = double.MinValue;
            double sumWeight = 0;
            double sumMins = 0;
            // 循环12个月的订单列表，算出每月的效率
            foreach (KeyValuePair<int, List<TeamProductionLineTime>> item in monthList)
            {
                double mins = 0;
                double weight = 0;
                // 算出每个月的上班时间，总产量
                foreach (TeamProductionLineTime order in item.Value)
                {
                    sumWeight += order.Weight;
                    sumMins += (order.EndTime - order.StartTime).TotalMinutes;
                    weight += order.Weight;
                    mins += (order.EndTime - order.StartTime).TotalMinutes;
                }
                double efficiency = 0;
                if (mins != 0 && weight != 0)
                    efficiency = Math.Round((weight / mins) * 60, 0);
                switch (item.Key)
                {
                    case 1:
                        obj.JanEfficiency = efficiency;
                        obj.JanHours = mins / 60;
                        obj.JanWeight = weight;
                        break;
                    case 2:
                        obj.FebEfficiency = efficiency;
                        obj.FebHours = mins / 60;
                        obj.FebWeight = weight;
                        break;
                    case 3:
                        obj.MarEfficiency = efficiency;
                        obj.MarHours = mins / 60;
                        obj.MarWeight = weight;
                        break;
                    case 4:
                        obj.AprEfficiency = efficiency;
                        obj.AprHours = mins / 60;
                        obj.AprWeight = weight;
                        break;
                    case 5:
                        obj.MayEfficiency = efficiency;
                        obj.MayHours = mins / 60;
                        obj.MayWeight = weight;
                        break;
                    case 6:
                        obj.JunEfficiency = efficiency;
                        obj.JunHours = mins / 60;
                        obj.JunWeight = weight;
                        break;
                    case 7:
                        obj.JulEfficiency = efficiency;
                        obj.JulHours = mins / 60;
                        obj.JulWeight = weight;
                        break;
                    case 8:
                        obj.AugEfficiency = efficiency;
                        obj.AugHours = mins / 60;
                        obj.AugWeight = weight;
                        break;
                    case 9:
                        obj.SepEfficiency = efficiency;
                        obj.SepHours = mins / 60;
                        obj.SepWeight = weight;
                        break;
                    case 10:
                        obj.OctEfficiency = efficiency;
                        obj.OctHours = mins / 60;
                        obj.OctWeight = weight;
                        break;
                    case 11:
                        obj.NovEfficiency = efficiency;
                        obj.NovHours = mins / 60;
                        obj.NovWeight = weight;
                        break;
                    case 12:
                        obj.DecEfficiency = efficiency;
                        obj.DecHours = mins / 60;
                        obj.DecWeight = weight;
                        break;
                }
                if (maxEffic < efficiency)
                    maxEffic = efficiency;
            }
            if (maxEffic != double.MinValue)
                obj.MaxEfficiency = Math.Round(maxEffic,0);
            if (sumMins != 0 && sumWeight != 0)
                obj.AvgEfficiency = Math.Round((sumWeight / sumMins) * 60, 0);
            obj.Hours = Math.Round(sumMins / 60,0);
            obj.Weight = Math.Round(sumWeight,0);
            return obj;
        }
    }
}
